package com.imooc.jdbc.hrapp.command;

import com.imooc.jdbc.common.DbUtils;
import com.imooc.jdbc.hrapp.entity.Employee;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class PaginationCommand implements Command{
    @Override
    public void execute() {


        Scanner in = new Scanner(System.in);
        System.out.println("请输入页号");
        int page = in.nextInt();

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        List<Employee> empList = new ArrayList();

        try {
            conn = DbUtils.getConnection();
            String sql = "select * from employee limit ?,10";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, (page-1) * 10);

            rs = pstmt.executeQuery();

            while (rs.next()){

                Employee emp = new Employee();

                emp.setId(rs.getInt("id"));
                emp.setDepname(rs.getString("dep_name"));
                emp.setEmpname(rs.getString("emp_name"));
                emp.setSalary(rs.getFloat("salary"));

                //JDBC获取日期使用java.sql.Date, 其继承自java.util.Date
                Date date = rs.getDate("hire_date");

                emp.setHiredate(date);

                empList.add(emp);

            }

            System.out.println(empList.size());

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            DbUtils.closeConnection(rs, pstmt, conn);
        }


    }
}
